package com.example.commen.view

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.fragment.app.FragmentManager
import com.blankj.utilcode.util.ScreenUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.example.commen.R
import com.example.commen.aop.SingleClick
import com.example.commen.base.BaseBottomSheetFrag
import com.example.commen.databinding.EmptyLayoutBinding
import com.example.commen.databinding.LayoutBottomlistBinding
import com.example.commen.databinding.LayoutTextBinding
import com.example.commen.interf.ICell.ICell2
import com.example.commen.other.ItemDecoration
import com.example.commen.utile.CommonUtils
import com.example.commen.utile.SingleClick
import com.example.commen.utile.StatusBarUtil
import kotlinx.coroutines.withContext

/**
 * @author zyju
 * @date 2024/3/26 15:26
 */
class BottomListSheetFrag : BaseBottomSheetFrag() {
    private lateinit var binding: LayoutBottomlistBinding
    private var itemClickListener: ICell2<Int, String>? = null
    private var titlename = ""
    private var isShowSearch = false

    var AlllistData = mutableListOf<String>()
    var listData = mutableListOf<String>()
    var adapter = object :
        BaseQuickAdapter<String, BaseDataBindingHolder<LayoutTextBinding>>(R.layout.layout_text) {
        override fun convert(holder: BaseDataBindingHolder<LayoutTextBinding>, item: String) {
            val binding = holder.dataBinding
            if (binding != null) {
                binding.solarDay.text = item
            }
        }
    }


    override fun getLayoutHeight(): Int {
        return if(layouheight==0)ScreenUtils.getScreenHeight() - ScreenUtils.getScreenHeight() * 1 / 5 else layouheight
    }

    override fun getLayoutResId(): Int {
        return R.layout.layout_bottomlist
    }
    private var layouheight = 0
    fun setHeight(height:Int){
        layouheight = height
    }
    override fun initView() {
        binding = LayoutBottomlistBinding.bind(rootView)
        adapter.setEmptyView(EmptyLayoutBinding.inflate(layoutInflater).root)
//        binding.recyLayoutBottomlistTitle.addItemDecoration(ItemDecoration(requireContext()))
        binding.recyLayoutBottomlistTitle.adapter = adapter
        adapter.setNewInstance(listData)
        adapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                dismiss()
                for ((i, _data) in AlllistData.withIndex()){
                    if (_data == listData[position]){
                        itemClickListener?.cell(i, listData[position])
                    }
                }
            }
        })
        SingleClick(binding.imgLayoutBottomlistDismiss) { dismiss() }
        binding.tvLayoutBottomlistTitle.text = titlename
        binding.etSearch.visibility = if (isShowSearch) View.VISIBLE else View.GONE
        binding.etSearch.addTextChangedListener(textSearchWatcher)
    }

    fun setOnItemClickListener(itemClickListeners: ICell2<Int, String>) {
        itemClickListener = itemClickListeners
    }

    fun setTitleName(title: String) {
        titlename = title
        if (title=="大纲"||title == "课程"){
            setIsShowSearch(true)
        }else{
            setIsShowSearch(false)
        }
        try {//未初始化时,避免闪退
            binding.tvLayoutBottomlistTitle.text = titlename
        }catch (e:Exception){
            e.printStackTrace()
        }
    }
    fun setIsShowSearch(isShow:Boolean) {
        isShowSearch = isShow
        try {//未初始化时,避免闪退
            binding.etSearch.visibility = if (isShowSearch) View.VISIBLE else View.GONE
        }catch (e:Exception){
            e.printStackTrace()
        }
    }

    fun show(list: List<String>, manager: FragmentManager, tag: String?) {
        if (isVisible) {
            dismiss()
        }
        AlllistData = list.toMutableList()
        listData = list.toMutableList()
        adapter.setNewInstance(listData)
        super.show(manager, tag)
    }

    var textSearchWatcher = object : TextWatcher {
        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
        override fun afterTextChanged(p0: Editable) {
            listData = AlllistData.filter { it.contains(p0) }.toMutableList()
            adapter.setNewInstance(listData)
        }

    }
}